package org.notabug.lifeuser.moviedb;

import android.content.Context;
import android.content.SharedPreferences;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/* 
* More advanced adapter specifically made to show a list of movies.
*/
public class MovieBaseAdapter extends BaseAdapter {
	private ArrayList<JSONObject> movieArrayList;
	private HashMap<String, String> genreHashMap;

	private Context context;

	private String listType;
	private String genreType;
	private int page;

	/**
	* Initializes the MovieBaseAdapter
	* @param listType 1 = movies; 0 = series.
	* @param genreType 1 = movie; 0 = series.
	* @param page The amount of pages that are needed.
	* @param context Context needed to do some of the jobs.
	*/
	public MovieBaseAdapter(ArrayList<JSONObject> movieList, HashMap<String, String> genreList, Context context) {
		// Get the right "type" of genres.
		if(genreType == "2" || genreType == null) {
			this.genreType = null;
		} else if(genreType == "1") {
			this.genreType = "movie";
		} else {
			this.genreType = "tv";
		}

		this.context = context;

		movieArrayList = movieList; 
		genreHashMap = genreList;
	}

	public void loadPage(ArrayList<JSONObject> movieList) {
		// Add the movieList to the current list of movies/series.
		movieArrayList = movieList;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// Reuse the old ListView items.
		if(convertView == null) {
			LayoutInflater inflater = (LayoutInflater) 
					context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.movie_cell, parent, false);
		}

		// Get the layout id's.
        TextView title = (TextView) convertView.findViewById(R.id.title);
        ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
        TextView description = (TextView) convertView.findViewById(R.id.description);
        TextView genre = (TextView) convertView.findViewById(R.id.genre);
        RatingBar rating = (RatingBar) convertView.findViewById(R.id.rating);

		// Get the needed movieList array.
		JSONObject movieDetails = movieArrayList.get(position);

		// Fills the views with movie details.
		try {	
			// Load the thumbnail with Picasso.
			Picasso.with(context).load("https://image.tmdb.org/t/p/w154" 
					+ movieDetails.getString("poster_path")).into(icon);

			// Load animation to make the loading of the image look smooth.
			Animation animation = AnimationUtils.loadAnimation(context,
					R.anim.fade_in_fast);
			icon.startAnimation(animation);

			// Check if the object has "title" if not,
			// it is a series and "name" is used.
			String name = (movieDetails.has("title")) ?
					movieDetails.getString("title") : movieDetails.getString("name");

			// Set the title and description.
			title.setText(name);
			description.setText(movieDetails.getString("overview"));

			// Divide the rating in two so it fits in the five stars.
			rating.setRating(Float.parseFloat(movieDetails.getString("vote_average")) / 2);

			// Remove the [ and ] from the String
			String genreIds = movieDetails.getString("genre_ids")
					.substring(1, movieDetails.getString("genre_ids")
					.length() - 1);

			// Split the String with ids and set them into an array.
			String[] genreArray = genreIds.split(",");

			// Get sharedPreferences in case the genreHashMap doesn't have the value
			SharedPreferences sharedPreferences = context.getSharedPreferences(
					"GenreList", Context.MODE_PRIVATE);

			// Add all the genres in one String.
			String genreNames = "";
			for(int i = 0; i < genreArray.length; i++) {
				if(genreHashMap.get(genreArray[i]) != null) {
					genreNames += ", " + genreHashMap.get(genreArray[i]);
				} else {
					genreNames += ", " + sharedPreferences.getString(genreArray[i], "");
				}
			}

			// Remove the first ", " from the string and set the text.
			genre.setText(genreNames.substring(2));
		} catch(JSONException e) {
			e.printStackTrace();
		}

		return convertView;
	}

	@Override
	public int getCount() {
		// Return the amount of items in the list.
		return movieArrayList.size();
	}

	@Override
	public Object getItem(int position) {
		return movieArrayList.get(position);
	} 

	@Override
	public long getItemId(int position) {
		// The id is the same as the position,
		// therefore returning the position is enough.
		return position;
	}
}
